package com.common.core.view;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.common.core.R;


/**
 * Created by lajin on 16/5/11.
 */
public class CommonTitleBar extends RelativeLayout implements View.OnClickListener {

    protected static final int TYPE_DEFAULT = 0x00;
    protected static final int TYPE_PHOTO_PICKER = 0x01;
    protected static final int TYPE_PHOTO_PREVIEW = 0x02;


    protected int type;
    protected String defaultTitle;
    protected boolean isShowBack;

    protected TextView tvTitle;
    protected ImageView ivBack;

    public CommonTitleBar (Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CommonTitleBar);
        type = array.getInt(R.styleable.CommonTitleBar_commontype, TYPE_DEFAULT);
        defaultTitle = array.getString(R.styleable.CommonTitleBar_commondefault_title);
        isShowBack = array.getBoolean(R.styleable.CommonTitleBar_is_commonshow_back, true);
        array.recycle();

        initViews();
    }

    private void initViews() {
        switch (type) {
            case TYPE_DEFAULT:
                inflate(getContext(), R.layout.common_view_titlebar_default, this);
                tvTitle = (TextView) findViewById(R.id.view_titlebar_default_title);
                ivBack = (ImageView) findViewById(R.id.view_titlebar_default_back);
                if(!TextUtils.isEmpty(defaultTitle)) {
                    tvTitle.setText(defaultTitle);
                }
                if(isShowBack) {
                    ivBack.setVisibility(View.VISIBLE);
                    ivBack.setOnClickListener(this);
                } else {
                    ivBack.setVisibility(View.GONE);
                }
                break;
            case TYPE_PHOTO_PICKER:
                inflate(getContext(), R.layout.common_view_titlebar_photo_picker, this);
                tvTitle = (TextView) findViewById(R.id.view_titlebar_photo_picker_title);
                findViewById(R.id.view_titlebar_photo_picker_back).setOnClickListener(this);
                break;
            case TYPE_PHOTO_PREVIEW:
                inflate(getContext(), R.layout.common_view_titlebar_photo_preview, this);
                tvTitle = (TextView) findViewById(R.id.view_titlebar_photo_preview_title);
                break;
        }
    }

    public void setOnClickListener(OnClickListener listener) {
        switch (type) {
            case TYPE_PHOTO_PICKER:
                findViewById(R.id.view_titlebar_photo_picker_ok).setOnClickListener(listener);
                findViewById(R.id.view_titlebar_photo_picker_title).setOnClickListener(listener);
                break;
            case TYPE_PHOTO_PREVIEW:
                findViewById(R.id.view_titlebar_photo_preview_back).setOnClickListener(listener);
                findViewById(R.id.view_titlebar_photo_preview_delete).setOnClickListener(listener);
                break;
        }
    }

    public void setTitle(String title) {
        tvTitle.setText(title);
    }

    public void setTitle(int title) {
        tvTitle.setText(title);
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.view_titlebar_default_back || i == R.id.view_titlebar_photo_picker_back) {
            ((Activity) getContext()).finish();
        }
    }
}
